import { API_LIST } from '../../../Api/Api'
import { commonTableList, BuiltInData } from '../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    OpenTwoRouter,
    ExamineButton,
    SaveExcelButton,
} from '../Common/options'
import { QualityProblemTypeTree } from '../Common/Conversion'
import { ProjectList } from '../Common/Conversion'

const optionsButton = ['yaxue.ljkjkf.com']

export const Quality = async ({ url, OptionsEvent, ClientData, Permission, actionRef }) => {
    const { hostname } = window.location
    const qualityProblemTypeTree = await QualityProblemTypeTree()
    Permission = Permission.find(v => v.targetUrl === url).children
    const { userUuid } = JSON.parse(sessionStorage.getItem('UserInfo'))

    const optionsData = {
        title: '操作',
        valueType: 'systemOperation',
        fixed: 'right',
        width: 560,
        hideInSearch: true,
        hideInAdded: true,
        hideInModify: true,
        render: (_, record) => {
            const { uuid, auditStatus, createUserUuid } = record
            return [
                <ModDataButton OptionsEvent={OptionsEvent} key="modify" title="修改" url={url} data={record} />,
                <OpenTwoRouter
                    key="TwoOne"
                    title="巡检详情"
                    twoTitle="质量巡检问题详情"
                    modal="TableList"
                    OptionsEvent={OptionsEvent}
                    other={{
                        page: 'InspectionProblem',
                        actionRef,
                        Permission,
                        oneData: record,
                        header: data.columns,
                        type: 'quality',
                    }}
                />,

                auditStatus !== -1 && auditStatus !== 0 && (
                    <OpenTwoRouter
                        key="TwoTwo"
                        title="整改详情"
                        twoTitle="质量巡检整改详情"
                        modal="TableList"
                        OptionsEvent={OptionsEvent}
                        other={{
                            page: 'InspectionDetails',
                            actionRef,
                            Permission,
                            oneData: record,
                            header: data.columns,
                            type: 'quality',
                        }}
                    />
                ),

                createUserUuid === userUuid && auditStatus === -1 && (
                    <ExamineButton
                        key="one"
                        title="提交问题"
                        data={{
                            actionRef,
                            OptionsEvent,
                            url: 'patQualitySubmit',
                            smallTitle: '提交质量巡检',
                        }}
                        options={[
                            { title: '提交', type: 'primary', emit: { path: [uuid] } },
                            { title: '取消', type: 'link', emit: 'off' },
                        ]}
                    />
                ),
                auditStatus === 0 && (
                    <ExamineButton
                        key="one"
                        title="接受整改"
                        data={{
                            actionRef,
                            OptionsEvent,
                            url: 'patQualityAccept',
                            smallTitle: '接受质量巡检整改',
                        }}
                        options={[
                            { title: '接受', type: 'primary', emit: { path: [uuid] } },
                            { title: '取消', type: 'link', emit: 'off' },
                        ]}
                    />
                ),
                auditStatus === 4 && (
                    <ExamineButton
                        key="one"
                        title="重新整改"
                        data={{
                            actionRef,
                            OptionsEvent,
                            url: 'patQualityAccept',
                            smallTitle: '重新接受质量巡检整改',
                        }}
                        options={[
                            { title: '重新接受', type: 'primary', emit: { path: [uuid] } },
                            { title: '取消', type: 'link', emit: 'off' },
                        ]}
                    />
                ),
                auditStatus === 2 && (
                    <ExamineButton
                        key="one"
                        title="审核整改"
                        data={{
                            actionRef,
                            OptionsEvent,
                            url: 'patQualityApproval',
                            smallTitle: '审核已整改的质量巡检',
                        }}
                        options={[
                            { title: '合格', type: 'primary', emit: { path: [uuid, 3] } },
                            { title: '不合格', type: 'link', danger: true, emit: { path: [uuid, 4] } },
                        ]}
                    />
                ),
                <DelDataButton OptionsEvent={OptionsEvent} key="del" title="删除" url={url} uuid={record.uuid} />,
                // 监理回执单
                optionsButton.includes(hostname) && (
                    <SaveExcelButton
                        key="oneOne"
                        title="监理回执单"
                        // OptionsEvent={OptionsEvent}
                        // data={{
                        //     save: true,
                        //     Uuid: uuid,
                        //     url: 'getSecurityGetSupervisionReceipt',
                        // }}
                        api={API_LIST()['getQualityGetSupervisionReceipt']}
                        data={{ uuid }}
                    />
                ),
                // 监理通知单
                optionsButton.includes(hostname) && (
                    <SaveExcelButton
                        key="oneTwo"
                        title="监理通知单"
                        // OptionsEvent={OptionsEvent}
                        // data={{
                        //     save: true,
                        //     Uuid: uuid,
                        //     url: 'getSecurityGetSupervisionWotice',
                        // }}
                        api={API_LIST()['getQualityGetSupervisionWotice']}
                        data={{ uuid }}
                    />
                ),
            ]
        },
    }

    const columnsA01 = [
        commonTableList.uuid,
        { ...commonTableList.SearchSort, selectType: 'Security' },
        commonTableList.BeginSecurityDate,
        commonTableList.EndSecurityDate,
        {
            ...commonTableList.projectUuid,
            valueEnum: await ProjectList(),
            hideInAdded: true,
            hideInModify: true,
            hideInTable: true,
            hideInSearch: !hostname.includes('jszx'),
        },
        { ...commonTableList.organizationUuid, valueEnum: ClientData.organization, title: '整改人' },
        { ...commonTableList.tableName, title: '巡检主题', width: 250 },
        {
            dataIndex: 'qualityProblemTypeUuid',
            title: '质量问题类别',
            valueType: 'treeSelect',
            fieldProps: {
                options: qualityProblemTypeTree,
                fieldNames: { label: 'tableName', value: 'uuid' },
            },
            rules: true,
            ellipsis: true,
            hideInSearch: true,
            width: 250,
        },
        {
            dataIndex: 'qualityDate',
            title: '巡检日期',
            valueType: 'Date',
            showTime: true,
            rules: true,
            width: 150,
            hideInSearch: true,
        },
        {
            dataIndex: 'limitDays',
            title: '整改天数',
            valueType: 'inputNumber',
            rules: true,
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'score',
            title: '分数',
            valueType: 'inputNumber',
            rules: true,
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'auditStatus',
            title: '状态',
            valueType: 'select',
            valueEnum: BuiltInData.PatrolStatus,
            hideInAdded: true,
            hideInModify: true,
            width: 150,
        },
        {
            dataIndex: 'problem',
            title: '问题描述',
            valueType: 'textarea',
            rules: true,
            ellipsis: true,
            width: 400,
            hideInSearch: true,
        },
        { ...commonTableList.createUserName, title: '下单人' },
        commonTableList.createTime,
        commonTableList.updateUserName,
        commonTableList.updateTime,
        optionsData,
    ]

    const gjColumns = [
        commonTableList.uuid,
        { ...commonTableList.SearchSort, selectType: 'Security' },
        commonTableList.BeginSecurityDate,
        commonTableList.EndSecurityDate,
        {
            ...commonTableList.projectUuid,
            valueEnum: await ProjectList(),
            hideInAdded: true,
            hideInModify: true,
            hideInTable: true,
            hideInSearch: !hostname.includes('jszx'),
        },
        {
            dataIndex: 'problem',
            title: '隐患名称',
            valueType: 'input',
            rules: true,
            ellipsis: true,
            width: 400,
            hideInSearch: true,
        },
        {
            dataIndex: 'qualityDate',
            title: '填报日期',
            valueType: 'Date',
            showTime: true,
            rules: true,
            width: 150,
            hideInSearch: true,
        },
        { ...commonTableList.tableName, title: '隐患位置', width: 250 },
        {
            dataIndex: 'qualityProblemTypeUuid',
            title: '隐患等级',
            valueType: 'treeSelect',
            fieldProps: {
                options: qualityProblemTypeTree,
                fieldNames: { label: 'tableName', value: 'uuid' },
            },
            rules: true,
            ellipsis: true,
            hideInSearch: true,
            width: 250,
        },
        {
            dataIndex: 'limitDays',
            title: '治理时限(天)',
            valueType: 'inputNumber',
            min: 0,
            rules: true,
            width: 120,
            hideInSearch: true,
            render: (_, e) => <div>{e.limitDays === 0 ? '立即整改' : e.limitDays}</div>,
        },
        { ...commonTableList.organizationUuid, valueEnum: ClientData.organization, title: '单位' },
        {
            dataIndex: 'auditStatus',
            title: '状态',
            valueType: 'select',
            valueEnum: BuiltInData.PatrolStatus,
            hideInAdded: true,
            hideInModify: true,
            width: 150,
        },
        {
            dataIndex: 'responsibleDepartment',
            title: '责任部门',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'responsiblePerson',
            title: '责任人',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'reCheckResult',
            title: '复查结果',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'acceptancePerson',
            title: '验收人',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'lng',
            title: '经度',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },
        {
            dataIndex: 'lat',
            title: '纬度',
            valueType: 'input',
            width: 100,
            hideInSearch: true,
        },

        { ...commonTableList.createUserName, title: '下单人' },
        commonTableList.createTime,
        commonTableList.updateUserName,
        commonTableList.updateTime,
        optionsData,
    ]

    const orgStatus = ['jtgjadmin.ljkjkf.com', 'jtgjadmin.hljsjtgj.com']

    let data = {
        pageSize: 10,
        params: { pageIndex: 1, OSecurityDate: 'desc@1' },
        get: API_LIST()[`get${url}`],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        options: [
            <AddDataButton OptionsEvent={OptionsEvent} url={url} type="primary" icon="add" title="新增质量巡检" />,
        ],
        columns: orgStatus.includes(hostname) ? gjColumns : columnsA01,
    }
    return data
}
